<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="datetime-date.html" />
<link rel="prev" href="node78.html" />
<link rel="parent" href="module-datetime.html" />
<link rel="next" href="datetime-date.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>5.1.2 timedelta Objects </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.1.1 available Types"
  href="node78.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="5.1 datetime  "
  href="module-datetime.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="5.1.3 date Objects"
  href="datetime-date.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node78.html">5.1.1 Available Types</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-datetime.html">5.1 datetime  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="datetime-date.html">5.1.3 date Objects</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION007120000000000000000"></a><a name="datetime-timedelta"></a>
<br>
5.1.2 <tt class="class">timedelta</tt> Objects 
</h2>

<p>
A <tt class="class">timedelta</tt> object represents a duration, the difference
between two dates or times.

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-610' xml:id='l2h-610' class="class">timedelta</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>days</var><big>[</big><var>, seconds</var><big>[</big><var>,
                             microseconds</var><big>[</big><var>, milliseconds</var><big>[</big><var>,
                             minutes</var><big>[</big><var>, hours</var><big>[</big><var>, weeks</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  All arguments are optional and default to <code>0</code>.  Arguments may
  be ints, longs, or floats, and may be positive or negative.

<p>
Only <var>days</var>, <var>seconds</var> and <var>microseconds</var> are stored
  internally.  Arguments are converted to those units:

<p>

<ul>
<li>A millisecond is converted to 1000 microseconds.
</li>
<li>A minute is converted to 60 seconds.
</li>
<li>An hour is converted to 3600 seconds.
</li>
<li>A week is converted to 7 days.
</li>
</ul>

<p>
and days, seconds and microseconds are then normalized so that the
  representation is unique, with

<p>

<ul>
<li><code>0 &lt;= <var>microseconds</var> &lt; 1000000</code>
</li>
<li><code>0 &lt;= <var>seconds</var> &lt; 3600*24</code> (the number of seconds in one day)
</li>
<li><code>-999999999 &lt;= <var>days</var> &lt;= 999999999</code>
</li>
</ul>

<p>
If any argument is a float and there are fractional microseconds,
  the fractional microseconds left over from all arguments are combined
  and their sum is rounded to the nearest microsecond.  If no
  argument is a float, the conversion and normalization processes
  are exact (no information is lost).

<p>
If the normalized value of days lies outside the indicated range,
  <tt class="exception">OverflowError</tt> is raised.

<p>
Note that normalization of negative values may be surprising at first.
  For example,

<p>
<div class="verbatim"><pre>
&gt;&gt;&gt; d = timedelta(microseconds=-1)
&gt;&gt;&gt; (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
</pre></div>
</dl>

<p>
Class attributes are:

<p>
<dl><dt><b><tt id='l2h-611' xml:id='l2h-611' class="member">min</tt></b></dt>
<dd>
  The most negative <tt class="class">timedelta</tt> object,
  <code>timedelta(-999999999)</code>.
</dl>

<p>
<dl><dt><b><tt id='l2h-612' xml:id='l2h-612' class="member">max</tt></b></dt>
<dd>
  The most positive <tt class="class">timedelta</tt> object,
  <code>timedelta(days=999999999, hours=23, minutes=59, seconds=59,
                  microseconds=999999)</code>.
</dl>

<p>
<dl><dt><b><tt id='l2h-613' xml:id='l2h-613' class="member">resolution</tt></b></dt>
<dd>
  The smallest possible difference between non-equal
  <tt class="class">timedelta</tt> objects, <code>timedelta(microseconds=1)</code>.
</dl>

<p>
Note that, because of normalization, <code>timedelta.max</code> &gt;
<code>-timedelta.min</code>.  <code>-timedelta.max</code> is not representable as
a <tt class="class">timedelta</tt> object.

<p>
Instance attributes (read-only):

<p>
<div class="center"><table class="realtable">
  <thead>
    <tr>
      <th class="center">Attribute</th>
      <th class="left"  >Value</th>
      </tr>
    </thead>
  <tbody>
    <tr><td class="center" valign="baseline"><code>days</code></td>
        <td class="left"  >Between -999999999 and 999999999 inclusive</td></tr>
    <tr><td class="center" valign="baseline"><code>seconds</code></td>
        <td class="left"  >Between 0 and 86399 inclusive</td></tr>
    <tr><td class="center" valign="baseline"><code>microseconds</code></td>
        <td class="left"  >Between 0 and 999999 inclusive</td></tr></tbody>
</table></div>

<p>
Supported operations:

<p>
<div class="center"><table class="realtable">
  <thead>
    <tr>
      <th class="center">Operation</th>
      <th class="left"  >Result</th>
      </tr>
    </thead>
  <tbody>
    <tr><td class="center" valign="baseline"><code><var>t1</var> = <var>t2</var> + <var>t3</var></code></td>
        <td class="left"  >Sum of <var>t2</var> and <var>t3</var>.
           Afterwards <var>t1</var>-<var>t2</var> == <var>t3</var> and <var>t1</var>-<var>t3</var>
           == <var>t2</var> are true.
          (1)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>t1</var> = <var>t2</var> - <var>t3</var></code></td>
        <td class="left"  >Difference of <var>t2</var> and <var>t3</var>.
           Afterwards <var>t1</var> == <var>t2</var> - <var>t3</var> and
           <var>t2</var> == <var>t1</var> + <var>t3</var> are true.
          (1)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>t1</var> = <var>t2</var> * <var>i</var> or <var>t1</var> = <var>i</var> * <var>t2</var></code></td>
        <td class="left"  >Delta multiplied by an integer or long.
           Afterwards <var>t1</var> // i == <var>t2</var> is true,
           provided <code>i != 0</code>.</td></tr>
    <tr><td class="center" valign="baseline"><code></code></td>
        <td class="left"  >In general, <var>t1</var> * i == <var>t1</var> * (i-1) + <var>t1</var> is true.
          (1)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>t1</var> = <var>t2</var> // <var>i</var></code></td>
        <td class="left"  >The floor is computed and the remainder (if any) is thrown away.
          (3)</td></tr>
    <tr><td class="center" valign="baseline"><code>+<var>t1</var></code></td>
        <td class="left"  >Returns a <tt class="class">timedelta</tt> object with the same value.
          (2)</td></tr>
    <tr><td class="center" valign="baseline"><code>-<var>t1</var></code></td>
        <td class="left"  >equivalent to <tt class="class">timedelta</tt>(-<var>t1.days</var>, -<var>t1.seconds</var>,
           -<var>t1.microseconds</var>), and to <var>t1</var>* -1.
          (1)(4)</td></tr>
    <tr><td class="center" valign="baseline"><code>abs(<var>t</var>)</code></td>
        <td class="left"  >equivalent to +<var>t</var> when <code>t.days &gt;= 0</code>, and to
           -<var>t</var> when <code>t.days &lt; 0</code>.
          (2)</td></tr></tbody>
</table></div>

Notes:

<p>
<dl>
<dt><strong>(1)</strong></dt>
<dd>This is exact, but may overflow.

<p>
</dd>
<dt><strong>(2)</strong></dt>
<dd>This is exact, and cannot overflow.

<p>
</dd>
<dt><strong>(3)</strong></dt>
<dd>Division by 0 raises <tt class="exception">ZeroDivisionError</tt>.

<p>
</dd>
<dt><strong>(4)</strong></dt>
<dd>-<var>timedelta.max</var> is not representable as a <tt class="class">timedelta</tt> object.
</dd>
</dl>

<p>
In addition to the operations listed above <tt class="class">timedelta</tt> objects
support certain additions and subtractions with <tt class="class">date</tt> and
<tt class="class">datetime</tt> objects (see below).

<p>
Comparisons of <tt class="class">timedelta</tt> objects are supported with the
<tt class="class">timedelta</tt> object representing the smaller duration considered
to be the smaller timedelta.
In order to stop mixed-type comparisons from falling back to the
default comparison by object address, when a <tt class="class">timedelta</tt> object is
compared to an object of a different type, <tt class="exception">TypeError</tt> is
raised unless the comparison is <code>==</code> or <code>!=</code>.  The latter
cases return <tt class="constant">False</tt> or <tt class="constant">True</tt>, respectively.

<p>
<tt class="class">timedelta</tt> objects are hashable (usable as dictionary keys),
support efficient pickling, and in Boolean contexts, a <tt class="class">timedelta</tt>
object is considered to be true if and only if it isn't equal to
<code>timedelta(0)</code>.

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.1.1 available Types"
  href="node78.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="5.1 datetime  "
  href="module-datetime.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="5.1.3 date Objects"
  href="datetime-date.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node78.html">5.1.1 Available Types</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-datetime.html">5.1 datetime  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="datetime-date.html">5.1.3 date Objects</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
